
import { _decorator,
    Component,
    Node,v3,
    Vec3,
    log,
    SystemEventType,
    EventKeyboard,
    macro,
    systemEvent,
    Vec2,
} from 'cc';
import {Joystick} from "./Joystick";
const { ccclass, property } = _decorator;


//
const tmpv3 = v3();
const moveFactor = 0.1;

@ccclass('Daidai')
export class Daidai extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    @property({type:Joystick})
    joystick:Joystick = null!;

    @property({type:Node})
    nd_camera:Node = null!;

    // @property({type:Node})

    // private
    moveDir = v3();
    offsetVec3 = v3();

    start () {
        //
        Vec3.subtract(this.offsetVec3,this.nd_camera.position,this.node.position);
    }

    onMove(moveDir:Vec2){
        log("onMove called!");
        // let forward = this.node.forward;
        // Vec3.set(tmpv3,moveDir.x * forward.x * moveDir.x,0,-forward.z * moveFactor * moveDir.y);
        // Vec3.add(tmpv3,this.node.position,tmpv3);
        // this.node.position = tmpv3;

        let forward = this.node.forward;
        Vec3.multiplyScalar(forward,forward,0.2);
        Vec3.add(tmpv3,this.node.position,forward);
        this.node.position = tmpv3;
    }

    protected update(dt: number) {

        this.joystick.getMoveDir(this.moveDir);
        if (this.moveDir.x != 0 || this.moveDir.y != 0 || this.moveDir.z != 0){
            // 先调整方向,再移动
            let newForward =  Vec3.set(tmpv3,this.moveDir.x,0,-this.moveDir.y);
            newForward.normalize();
            this.node.forward =  newForward;

            Vec3.multiplyScalar(tmpv3,newForward,0.05);

            // 添加上移动
            let newPos =  Vec3.add(tmpv3,this.node.position,tmpv3);
            this.node.position = newPos;
        }
    }

    protected lateUpdate(dt: number) {
        Vec3.add(tmpv3,this.node.position,this.offsetVec3);
        this.nd_camera.position = tmpv3;
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
